package cn.example.vip.mall.canal.dynamic;

import cn.example.vip.mall.canal.spring.SpringContext;
import cn.example.vip.mall.page.feign.SeckillPageFeign;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.time.Instant;

public class DynamicJob implements SimpleJob {
	
	/**
	 * 定时任务的具体方法
	 * @param shardingContext
	 */
	@Override
	public void execute(ShardingContext shardingContext) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String now = sdf.format(Date.from(Instant.now()));
		String taskParams = shardingContext.getJobParameter();
		System.out.println("任务参数:" + taskParams + ", 当前时间:" + now);
		delete(taskParams);
	}
	
	/**
	 * 删除页面
	 * @param actId
	 */
	public void delete(String actId){
		// 该任务由其他容器创建，springboot无法直接注入Bean，因此需要Spring的静态变量
		Object bean = SpringContext.getBean(SeckillPageFeign.class);
		SeckillPageFeign seckillPageFeign = (SeckillPageFeign) bean;
		// 根据 activityId 删除响应的秒杀商品页面
		seckillPageFeign.delByActivityId(actId);
	}
	
}
